Skip to main content

Basic data type

Variables

Before going to data type we need to know about variables. Variables store data. Every variable has a name called identifier. Variables are declared in three types var, let, and const in JavaScript.

  • Variables declared using var keyword are function-scoped. That means, if they are declared outside the scope of a function, it will be available globally. They can be re-declared and updated.

  • Variables declared using let keyword are block-scoped. variable declared in a block with let is only available for use within that block. They can be updated but cannot be re-rendered.

  • Variables declared using const are similiar to the let variable declaration. They can neither be re-declared or updated.

Syntax

var x = 1;
let y = "Hello world";
const z = 3.14;

Work out

var x = 2;
var y = 3,
z = 4;
console.log(x, y, z);
// expected output: 2, 3, 4
Note

Multiple variables can be declared in single line in JavaScript.

Basic data types

Every variable has a data type, this data type indicates what kind of data that variable holds. JavaScript is a loosely typed language. Variables are not directly associated with a data type. The data type of a variable is assigned when the variable is declared. There are three basic data types in JavaScript, they are:

  • Strings: A string is a list of characters enclosed in single or double quotes.
  • Numbers: Numbers are integers or floating point numerical values.
  • Booleans: Booleans take either true or false.

Two special data types are:

  • Undefined: Value for a variable is not defined.
  • Null: Set value for a variable as null.

Using typeof keyword, the data type of a variable can be checked.

var stringData = "Hello world!";
var numericalData = 3.14;
var booleanData = true;
var value = undefined;
var nullValue = null;
console.log(typeof stringData);
Note

Basic data types are also known as primitive data types.